using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using BattleCity.Rendering;
using BattleCity.GameLogic;

#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif

namespace BattleCity
{
    
    public class SpaceTankGame : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;

        public GameStateUpdater gameLogic;
        public Renderer renderer;
   
        public SpaceTankGame()
        {
            graphics = new GraphicsDeviceManager(this);
           
            #if !WINDOWS_PHONE   
            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferMultiSampling = false;
            graphics.IsFullScreen = false;
            Mouse.WindowHandle = Window.Handle;
            IsMouseVisible = true;
            #else
            graphics.PreferMultiSampling = false;
            graphics.IsFullScreen = true;
            #endif

            #if WINDOWS_PHONE
            #if DEBUG
            Guide.SimulateTrialMode = true;
            #endif
            #endif

            graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft;
            graphics.ApplyChanges();
            Content.RootDirectory = "Content";
        }


        protected override void Initialize()
        {          
            this.onStart();
            
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        protected override void OnDeactivated(object sender, EventArgs args)
        {
            if (gameLogic != null)
            {
                GameLogic.Xml.SerializableGameState gameState = new GameLogic.Xml.SerializableGameState();
                gameState.setSerializableGameState(gameLogic.levelNumber);
                gameLogic.SaveGameState(gameState);
            }

            Options.saveOptions();

            base.OnDeactivated(sender, args);
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            base.Draw(gameTime);
        }

        private void saveScore(IAsyncResult result)
        {
            if (result.IsCompleted)
            {
                String name = Guide.EndShowKeyboardInput(result);
                if (name != null)
                {
                    if (name == "") name = "unknow";
                    if (name.Length > 10) Scores.Save(new Scores.Score(name.Substring(0, 10), GameState.gameState.CompletedLevels.Value));
                    else Scores.Save(new Scores.Score(name, GameState.gameState.CompletedLevels.Value));
                }
            }

            this.switchToMainMenu(true);
        }

        public void showScoreKeyboard()
        {
            Options.tomboneState = CurrentGameState.Menu;

            this.clearComponents();
            Sound.stopMusic();
            Sound.stopSoundsFx();

            #if WINDOWS_PHONE
            if ((Scores.savedScores.scores.Count < 10))
                Guide.BeginShowKeyboardInput(PlayerIndex.One, "Top 10 Score!", "Choose a Nickname (max 10 characters.)", "", saveScore, null);
            else
            {
                if (GameState.gameState.CompletedLevels.Value > Scores.savedScores.scores[9].completedLevels)
                    Guide.BeginShowKeyboardInput(PlayerIndex.One, "Top 10 Score!", "Choose a Nickname (max 10 characters.)", "", saveScore, null);
            }
            #endif
        }
    }
}
